#include "CDelegate.h"
#include <QWidget>
#include <QLabel>
#include <QPushButton>
#include <QHBoxLayout>
#include <QVariant>
#include "CModel/CModel.h"

MulBtnWiget::MulBtnWiget(const QModelIndex &index, QWidget *parent/* = nullptr*/)
    : QWidget(parent), _index(index)
{
    QHBoxLayout *pLayout = new QHBoxLayout();
    _pButton1 = new QPushButton("red");
    pLayout->addWidget(_pButton1);
    _pButton2 = new QPushButton("green");
    pLayout->addWidget(_pButton2);
    _pButton3 = new QPushButton("reset");
    pLayout->addWidget(_pButton3);
    pLayout->setContentsMargins(0, 0, 0, 0);
    this->setLayout(pLayout);
    this->setContentsMargins(0, 0, 0, 0);
    _pButton1->setEnabled(false);
    _pButton2->setEnabled(false);
    _pButton3->setEnabled(false);
    _pButton1->setStyleSheet("color: red;");
    _pButton2->setStyleSheet("color: green;");
    _pButton3->setStyleSheet("color: black;");

    connect(_pButton1, &QPushButton::clicked, this, [this](){
        emit btnColor("red", _index.row());
    });
    connect(_pButton2, &QPushButton::clicked, this, [this](){
        emit btnColor("green", _index.row());
    });
    connect(_pButton3, &QPushButton::clicked, this, [this](){
        emit btnColor("black", _index.row());
    });
}

void MulBtnWiget::setBtnData(const QVariant &data)
{
    int type = qvariant_cast<int>(data);
    if (type <= 3) {
        _pButton1->setEnabled(true);
        _pButton2->setEnabled(true);
        _pButton3->setEnabled(true);
        _pButton1->setStyleSheet("color: red;");
        _pButton2->setStyleSheet("color: green;");
    } else if (type >3 && type <=4) {
        _pButton1->setEnabled(false);
        _pButton1->setStyleSheet("color: black;");
        _pButton2->setEnabled(true);
        _pButton3->setEnabled(true);
    } else {
        _pButton1->setEnabled(true);
        _pButton2->setEnabled(false);
        _pButton2->setStyleSheet("color: black;");
        _pButton3->setEnabled(true);
    }
}

COpratorDelegate::COpratorDelegate(QObject *parent/* = nullptr*/,
                                   const ColType &type/*=STRING*/)
    : QStyledItemDelegate(parent),
      _colType(type)
{
}

QWidget *COpratorDelegate::createEditor(QWidget *parent,
                                        const QStyleOptionViewItem &option,
                                        const QModelIndex &index) const
{
    if (_colType == STRING) {
        return QStyledItemDelegate::createEditor(parent, option, index);
    } else if (_colType == COMBOBOX) {
        auto *pCbx = new QComboBox(parent);
        pCbx->addItems({"test1", "test1", "test2", "test3"});
        return pCbx;
    } else if (_colType == PROGRESSBAR) {
        auto *pProgressbar = new QProgressBar(parent);
        pProgressbar->setRange(0, 10);
        return pProgressbar;
    } else if (_colType == MULBTN) {
        MulBtnWiget *pMulBtnWidget = new MulBtnWiget(index, parent);
        connect(pMulBtnWidget, &MulBtnWiget::btnColor, this,
                [this](const QString &color, const int &row){
            // index.model()返回的模型不一定是自定义的数据模型，可能是代理模型
            if (_pModel == nullptr) { return; }
            _pModel->setRowColor(row, color);
        });
        return pMulBtnWidget;
    } else {
        return QStyledItemDelegate::createEditor(parent, option, index);
    }
}

void COpratorDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
    if (_colType == STRING) {

    } else if (_colType == COMBOBOX) {
        QComboBox *pWidget = qobject_cast<QComboBox*>(editor);
        auto tmp = QString::number(index.data().toInt());
        if (pWidget->findText(tmp) < 0) {
            pWidget->addItem(tmp);
        }
        pWidget->setCurrentText(tmp);
    } else if (_colType == PROGRESSBAR) {
        QProgressBar *pWidget = qobject_cast<QProgressBar*>(editor);
        pWidget->setValue(index.data().toInt());
    } else if (_colType == MULBTN) {
        MulBtnWiget *pWidget = qobject_cast<MulBtnWiget*>(editor);
        pWidget->setBtnData(index.data());
    } else {

    }
}

void COpratorDelegate::setModelData(QWidget *editor,
                                    QAbstractItemModel *model,
                                    const QModelIndex &index) const
{
    QStyledItemDelegate::setModelData(editor, model, index);
}
